package com.hwg.shiliupro.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hwg.shiliupro.dao.HomePageMapper;
import com.hwg.shiliupro.dao.UserMapper;
import com.hwg.shiliupro.finals.Code;
import com.hwg.shiliupro.po.HomePage;
import com.hwg.shiliupro.po.User;
import com.hwg.shiliupro.service.HomePageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class HomePageServiceImpl implements HomePageService {
    @Autowired
    private HomePageMapper homePageMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 保存用户头像
     *
     * @param base64 base64
     * @param userId 用户id
     * @return {@link Boolean}
     */
    @Override
    public Boolean saveImg(String base64,Integer userId) {
        HomePage homePage = new HomePage();
        homePage.setImg(base64);
        QueryWrapper<HomePage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        return homePageMapper.update(homePage,queryWrapper) > 0;
    }

    /**
     * 通过userId查找用户头像base64
     *
     * @param userId 用户id
     * @return {@link String}
     */
    @Override
    public String getImgByUserId(Integer userId) {
        QueryWrapper<HomePage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        HomePage homePage = homePageMapper.selectOne(queryWrapper);
        return homePage.getImg();
    }

    /**
     * 提交个人主页信息供 审核
     *
     * @param homePage 主页
     * @return {@link Boolean}
     */
    @Override
    public Boolean sumbitInfo(HomePage homePage) {
        homePage.setStatus(Code.EXAMINE_STAY);
        int count = homePageMapper.updateById(homePage);
        return count > 0;
    }

    /**
     * 管理员审核信息
     *
     * @param homePage 主页
     * @return {@link Boolean}
     */
    @Override
    public Boolean checkInfo(HomePage homePage,Integer flag) {
        if(flag == 1){
            //审核通过 修改homePage表的status  且修改真正user表的数据
            homePage.setStatus(Code.EXAMINE_OK);
            User user = new User();
            user.setId(homePage.getId());
            user.setNumber(homePage.getNumber());
            user.setName(homePage.getName());
            user.setSex(homePage.getSex());
            user.setAcademy(homePage.getAcademy());
            user.setSgroup(homePage.getSgroup());
            user.setMajor(homePage.getMajor());
            user.setCampus(homePage.getCampus());
            user.setMail(homePage.getMail());
            user.setPhone(homePage.getPhone());
            userMapper.updateById(user);
        }
        else{
            //审核不通过  只把homePage表的status 改为 2 即Code.EXAMINE_ERR
            homePage.setStatus(Code.EXAMINE_ERR);
        }
        homePageMapper.updateById(homePage);
        return true;
    }

    /**
     * 获取个人主页信息通过id
     * @param id
     * @return {@link HomePage}
     */
    @Override
    public HomePage getInfoById(Integer id) {
        HomePage homePage = homePageMapper.selectById(id);
        return homePage;
    }

    /**
     * 获取所有待审核的个人主页信息
     *
     * @return {@link List}<{@link HomePage}>
     */
    @Override
    public List<HomePage> getAll() {
        QueryWrapper<HomePage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        return homePageMapper.selectList(queryWrapper);
    }


}
